package com.xlhl.init.model.enums;

import com.xlhl.init.exceptipn.BusinessException;
import com.xlhl.init.utils.ThrowUtil;
import lombok.Getter;

/**
 * 账号启用状态
 *
 * @author xlhl
 */
@Getter
public enum AccountStatusEnums {
    /**
     * 启用
     */
    ENABLE(1, "启用"),

    /**
     * 弃用
     */
    DISABLE(0, "弃用"),
    ;

    private final int code;

    private final String description;

    AccountStatusEnums(int code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * @param code
     * @return
     */
    public static AccountStatusEnums getByCode(int code) {
        return getByCode(code, false);
    }

    /**
     * @param code       编码
     * @param returnNull 没找到是否返回空值
     * @return
     */
    public static AccountStatusEnums getByCode(int code, boolean returnNull) {
        for (AccountStatusEnums accountStatusEnums : AccountStatusEnums.values()) {
            if (accountStatusEnums.getCode() == code) {
                return accountStatusEnums;
            }
        }

        ThrowUtil.throwIf(!returnNull, new BusinessException("未知的账号状态"));

        return null;
    }
}
